﻿$(document).ready(function () {

    $(".txtBrows").blur(function () {

        var offset = $(this).offset();
        var xVal = offset.left;
        var yVal = offset.top;
        var sUrl = $(this).val();
        if (sUrl !== 'http://') {
            ShowHideLoading(true, xVal, yVal + 30);

            $.ajax({
                url: "Clustering.aspx/GetContentFromUrl",
                type: "POST",
                data: JSON.stringify({ 'sUrl': sUrl }),
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) { GetContentSucc(data, 'a'); },
                error: function () { }
            });

        }

    });

    //$('.txtBrows').bind('paste', function() {
    //  alert('ssssssssss');
    //         var offset = $(this).offset();
    //        var xVal = offset.left;
    //        var yVal = offset.top;
    //        var sUrl = $(this).val();
    //        ShowHideLoading(true, xVal, yVal + 30);

    //        $.ajax({
    //            url: "Clustering.aspx/GetContentFromUrl",
    //            type: "POST",
    //            data: JSON.stringify({ 'sUrl': sUrl }),
    //            dataType: "json",
    //            contentType: "application/json; charset=utf-8",
    //            success: function (data) { GetContentSucc(data, 'a'); },
    //            error: function () { }
    //        });
    //})​;




    $('#file_upload').uploadify({
        'uploader': 'Content/uploadify.swf',
        'script': 'FileHandler.ashx',
        'cancelImg': 'Content/Images/cancel.png',
        'folder': 'Uploads',
        'buttonText': 'Browse',
        'multi': false,
        'fileDataName': 'Filedata',
        'fileExt': '*.txt;*.doc;*.docx;',
        'fileDesc': 'Web Image Files (.txt, .doc, .docx)',
        //'sizeLimit': 1048576,
        'onSelect': function (event, ID, fileObj) {
            $("#notice").hide();
        },
        'onProgress': function (event, ID, fileObj, data) {
            //$("span[id$='spResult']").text("Loading ... ");
            //$("#loading-div").show();
            $("#notice").hide();
        },
        'onComplete': function (event, ID, fileObj, response, data) {
            var curTargetID = event.target.id;
            var sRes = response;
            var fileType = fileObj.type;

            $("textarea[id$='txtContent']").text(response);
            $("#loading-div").hide();
            $("#notice").show();
        },
        'onError': function (event, ID, fileObj, errorObj) {
            $("span[id$='spResult']").text("Có lỗi rồi: " + errorObj.message);
            //$("span[id$='spResult']").text("Có lỗi rồi mày ơi. Thử lại xem sao");
            //UploadError();
            $("#loading-div").hide();
            $("#notice").show();
        }
    });

    $("#btnSaveImage").click(function () {
        startUpload();
        $("#file_upload").uploadifyUpload();
    });


});

function startUpload() {
    $('#file_upload').uploadifySettings('scriptData', { 'name': $("input[id$='file_upload']").val() });
}


function ShowHideLoading(bCheck, xVal, yVal) {
    if (bCheck === true) {
        $('.loading').show();
        $('.loading').css({ top: yVal, left: xVal });
    }
    else
        $('.loading').hide();

}

function GetContentSucc(data, a) {
    
    $("textarea[id$='txtContent']").val(data.d);

    ShowHideLoading(false,0,0)
}
